package com.kzj.laimeimei.fragment;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ab.util.AbViewUtil;
import com.kzj.laimeimei.R;
import com.kzj.laimeimei.activity.UserInfoActivity;
import com.kzj.laimeimei.beans.CropImageBean;
import com.kzj.laimeimei.beans.UserAboutItemBean;
import com.kzj.laimeimei.beans.UserBean;
import com.kzj.laimeimei.recyclerCard.basic.MaterialListAdapter;
import com.kzj.laimeimei.recyclerCard.basic.MaterialListView;
import com.kzj.laimeimei.recyclerCard.card.other.AlphaCard;
import com.kzj.laimeimei.recyclerCard.card.userAbout.UserAboutItemCard;
import com.kzj.laimeimei.recyclerCard.card.userAbout.UserAboutLogoCard;
import com.umeng.analytics.MobclickAgent;

import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/10/9.
 */
public class UserAboutFragment extends BaseFragment {
    private MaterialListView materialListView;
    private View holderView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (holderView == null) {
            holderView = inflater.inflate(R.layout.template_materlistview, container, false);
            findViewByIds();
            initViews();
            EventBus.getDefault().register(this);
        }
        ViewGroup parent = (ViewGroup) holderView.getParent();
        if (parent != null) {
            parent.removeView(holderView);
        }
        return holderView;
    }

    @Override
    public void findViewByIds() {
        materialListView = (MaterialListView) holderView.findViewById(R.id.material_listview);
    }

    public void onEventMainThread(UserBean userBean) {
        materialListView.getAdapter().notifyDataSetChanged();
    }

    @Override
    public void initViews() {
        UserAboutLogoCard userAboutLogoCard = new UserAboutLogoCard(getActivity());
        materialListView.add(userAboutLogoCard);
        for (int i = 0; i < 7; i++) {
            UserAboutItemCard userAboutItemCard = new UserAboutItemCard(getActivity());
            UserAboutItemBean userAboutItemBean = new UserAboutItemBean();
            switch (i) {
                case 0:
                    userAboutItemBean.setPosition(i);
                    userAboutItemBean.setIsNeedLine(true);
                    userAboutItemBean.setItemName("我的优惠劵");
                    break;
                case 1:
                    userAboutItemBean.setPosition(i);
                    userAboutItemBean.setIsNeedLine(false);
                    userAboutItemBean.setItemName("地址管理");
                    break;
                case 2:
                    userAboutItemBean.setPosition(i);
                    userAboutItemBean.setIsNeedLine(true);
                    userAboutItemBean.setItemText(getVersion());
                    userAboutItemBean.setItemName("检查更新");
                    break;
                case 3:
                    userAboutItemBean.setPosition(i);
                    userAboutItemBean.setIsNeedLine(true);
                    userAboutItemBean.setItemName("联系客服");
                    break;
                case 4:
                    userAboutItemBean.setPosition(i);
                    userAboutItemBean.setIsNeedLine(true);
                    userAboutItemBean.setItemName("意见反馈");
                    break;
                case 5:
                    userAboutItemBean.setPosition(i);
                    userAboutItemBean.setIsNeedLine(true);
                    userAboutItemBean.setItemName("服务说明");
                    break;
                case 6:
                    userAboutItemBean.setPosition(i);
                    userAboutItemBean.setIsNeedLine(false);
                    userAboutItemBean.setItemName("关于来美美吧");
                    break;
            }
            userAboutItemCard.setUserAboutItemBean(userAboutItemBean);
            materialListView.add(userAboutItemCard);
        }
        AlphaCard alphaCard = new AlphaCard(getActivity());
        alphaCard.setIsNeedBottomLine(true);
        alphaCard.setIsNeedTopLine(true);
        alphaCard.setItemHeight(AbViewUtil.scaleValue(getActivity(), 20));
        materialListView.add(1, alphaCard);
        materialListView.add(4, alphaCard);
        AlphaCard alphaCard1 = new AlphaCard(getActivity());
        alphaCard1.setIsNeedTopLine(true);
        alphaCard1.setItemHeight(AbViewUtil.scaleValue(getActivity(), 20));
        materialListView.add(alphaCard1);
    }

    //更新头像
    public void onEventMainThread(CropImageBean cropImageBean) {
        //图片回调涉及多个类，防止无关类加载
        if (UserInfoActivity.class.getCanonicalName().contains(cropImageBean.getActivityString())) {
            MaterialListAdapter materialListAdapter = (MaterialListAdapter) (materialListView.getAdapter());
            materialListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void fillArrayInList() {

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (materialListView != null)
                materialListView.getAdapter().notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public String getVersion() {
        try {
            PackageManager manager = getActivity().getPackageManager();
            PackageInfo info = manager.getPackageInfo(getActivity().getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MainActivity"); //统计页面
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainActivity");
    }
}
